//
//  ThirdPayUtil.m
//  ScholarsBridge
//
//  Created by guggle on 17/2/14.
//  Copyright © 2017年 mingthink. All rights reserved.
//

#import "ThirdPayUtil.h"
#import "WXApi.h"
#import "ThirdShareUtil.h"

static NSString *wxAppID = @"wx72ff88c310c8ec19";//正式.wxaf87d110c9fdce4b,测试.wx5a374df688060992
//static NSString *wxAppID = @"";

@implementation ThirdPayUtil
+ (void)registerWXPay{
    [WXApi registerApp:wxAppID withDescription:@"wxpay"];
}
#pragma mark - 微信支付结果回调
+ (void)onResp:(BaseResp*)resp withWindow:(UIWindow *)window{
    if ([resp isKindOfClass:[PayResp class]]){
        NSString *strMsg= nil;
        PayResp *response=(PayResp*)resp;
        switch(response.errCode){
            case WXSuccess:
                //服务器端查询支付通知或查询API返回的结果再提示成功
                [[NSNotificationCenter defaultCenter] postNotificationName:wxPayCallBack object:nil];
                strMsg = @"支付成功！";
                break;
            case WXErrCodeUserCancel:
                strMsg = @"用户取消支付！";
                break;
            default:
                strMsg =[NSString stringWithFormat:@"支付失败：retcode=%d，retstr=%@",resp.errCode,resp.errStr];
                DeLog(@"支付失败：retcode=%d，retstr=%@",resp.errCode,resp.errStr);
                break;
        }
        [window makeToast:strMsg];
    }
}

#pragma mark- application 代理
+ (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options withDelegate:(id<WXApiDelegate>) delegate{
//    if ([url.host isEqualToString:@"safepay"]) {
//        
//        //跳转支付宝钱包进行支付，处理支付结果
//        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
//            DeLog(@"result = %@",resultDic);
//            [[NSNotificationCenter defaultCenter] postNotificationName:kAliPayCallBack object:resultDic];
//        }];
//    }
    if([[url absoluteString] rangeOfString:[NSString stringWithFormat:@"%@://pay",wxAppID]].location == 0){ //你的微信开发者appid
        return [WXApi handleOpenURL:url delegate:delegate];
    }
    //这里是微信登录授权
    if ([[url absoluteString] rangeOfString:[NSString stringWithFormat:@"%@://oauth",[ThirdShareUtil wxAppID]]].location == 0){
        [[NSNotificationCenter defaultCenter] postNotificationName:wxLoginCallBack object:url];
    }
    return  YES;
}

+ (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation withDelegate:(id<WXApiDelegate>) delegate{
//    if ([url.host isEqualToString:@"safepay"]) {
//        //跳转支付宝钱包进行支付，处理支付结果
//        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
//            DeLog(@"result = %@",resultDic);
//            [[NSNotificationCenter defaultCenter] postNotificationName:kAliPayCallBack object:resultDic];
//        }];
//    }
    if([[url absoluteString] rangeOfString:[NSString stringWithFormat:@"%@://pay",wxAppID]].location == 0){ //你的微信开发者appid
        return [WXApi handleOpenURL:url delegate:delegate];
    }
    
    return  YES;
}

+ (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url withDelegate:(id<WXApiDelegate>) delegate{
    if([[url absoluteString] rangeOfString:[NSString stringWithFormat:@"%@://pay",wxAppID]].location == 0){
        return [WXApi handleOpenURL:url delegate:delegate];
    }
    else{
        return YES;
    }
}

#pragma mark- 支付宝支付
+ (void)alipayWithOrderString:(NSString *)orderStr resultBlock:(payResultBlock)resultBlock{
    if ([orderStr length] == 0)
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示"message:@"订单为空。"delegate:self cancelButtonTitle:@"确定"otherButtonTitles:nil];
        [alert show];
        return;
    }
    
    //应用注册scheme,在AlixPayDemo-Info.plist定义URL types
    NSString *appScheme = @"alipayGaokaoBridge";
    DeLog(@"------alipayOrder:%@",orderStr);
//    [[AlipaySDK defaultService] payOrder:orderStr fromScheme:appScheme callback:^(NSDictionary *resultDic) {
//        if (resultBlock) {
//            resultBlock(resultDic);
//        }
//    }];
}

#pragma mark- 微信支付
+ (void)wxpayWithOrderDictionary:(NSDictionary *)orderDict{
    //调起微信支付l,....
    if (![WXApi isWXAppInstalled]) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"未安装微信,调用失败" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView show];
        return;
    }
    PayReq* req             = [[PayReq alloc] init];
    req.partnerId           = orderDict[@"Mchid"];
    req.prepayId            = orderDict[@"PrepayId"];
    req.nonceStr            = orderDict [@"NonceStr"];
    req.timeStamp           = [orderDict[@"Timestamp"] intValue];
    req.package             = @"Sign=WXPay";
    req.sign                = orderDict[@"Sign"];
    [WXApi sendReq:req];
    
}

@end
